package com.jzy.bxb.model.mongo.dao;

import org.mongodb.morphia.Datastore;
import org.mongodb.morphia.Morphia;
import org.mongodb.morphia.dao.BasicDAO;
import org.mongodb.morphia.query.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.jzy.bxb.core.db.mongo.AbsMongoManager;
import com.jzy.bxb.model.mongo.entity.Address;
import com.jzy.bxb.model.mongo.entity.User;
import com.mongodb.MongoClient;

/**
 * 用户
 *
 * @author JiangZhiYong
 * @date 2017-06-22 QQ:359135103
 */
@Repository
public class UserDao extends BasicDAO<User, Long> {
	private static volatile UserDao userDao;
	

	@Autowired
	public UserDao(Datastore ds) {
		super(ds);
	}

	public UserDao(AbsMongoManager mongoManager) {
		super(User.class, mongoManager.getMongoClient(), mongoManager.getMorphia(),
				mongoManager.getMongoConfig().getDbName());
	}

	public UserDao(Class<User> entityClass, MongoClient mongoClient, Morphia morphia, String dbName) {
		super(entityClass, mongoClient, morphia, dbName);
	}

	public static UserDao init(AbsMongoManager mongoManager) {
		if (userDao == null) {
			synchronized (UserDao.class) {
				if (userDao == null) {
					userDao = new UserDao(mongoManager);
				}
			}
		}
		return userDao;
	}

	public static User getById(long id) {
		Query<User> query = userDao.createQuery().filter("id", id);
		return query.get();
	}

	/**
	 * 会话sessionID查询
	 * @param sid
	 * @return
	 */
	public static User getBySid(String sid) {
		Query<User> query = userDao.createQuery().filter("sid",sid);
		return query.get();
	}

	/**
	 * 账号查询
	 * 
	 * @param userName
	 * @return
	 */
	public static User getByAccount(String userName) {
		Query<User> query = userDao.createQuery().filter("userName", userName);
		return query.get();
	}

	public static void saveUser(User user) {
		userDao.save(user);
	}
	
}
